package sdk.pay;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.util.List;

import sdk.pay.utils.PayLogUtil;

/**
 * Created by Joe on 2017/5/26 0026 17:32.
 * Pay by android browser
 */

final class PayBrowser {
    private Activity mActivity;

    PayBrowser(Activity activity) {
        mActivity = activity;
    }

    @SuppressWarnings("WrongConstant")
    void doPay(String url) {
        Uri uri = Uri.parse(url.trim());
        Intent urlintent = new Intent(Intent.ACTION_VIEW, uri);
        PackageManager pm = mActivity.getPackageManager(); //get packagemanager
        List<ResolveInfo> queryactivities = pm.queryIntentActivities(urlintent,
                PackageManager.GET_INTENT_FILTERS);
        PayLogUtil.logFifth(queryactivities.toString());
        for (ResolveInfo info : queryactivities) {
            PayLogUtil.logFifth(info.toString());
            switch (info.activityInfo.packageName) {
                case "com.android.chrome":
                case "com.oupeng.mini.android":
                case "com.oupeng.browser":
                case "com.opera.browser":
                case "com.qihoo.browser":
                case "sogou.mobile.explorer":
                case "com.UCMobile":
                case "com.tencent.mtt":
                case "org.mozilla.firefox":
                    urlintent.setClassName(info.activityInfo.packageName,
                            info.activityInfo.name);
                    break;
                default:
                    if (queryactivities.indexOf(info) == queryactivities.size() - 1) {
                        urlintent.setClassName(info.activityInfo.packageName,
                                info.activityInfo.name);
                        break;

                    }
                    continue;
            }
            break;
        }
        urlintent.addCategory(Intent.CATEGORY_DEFAULT);
        urlintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mActivity.startActivity(urlintent);
    }
}
